import tkinter as tk


class B(tk.Frame):
    def __init__(self, master, shared_var):
        super().__init__(master)
        self.shared_var = shared_var

    def process_data(self, data):
        # 处理数据
        result = data * 2
        # 设置共享变量的值
        self.shared_var.set(result)


class A(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        # 创建共享变量
        self.shared_var = tk.IntVar()
        # 设置变量跟踪
        self.shared_var.trace_add("write", self.on_result_change)

        # 创建 B 实例
        self.b_instance = B(master, self.shared_var)

        # 创建发送按钮
        self.send_button = tk.Button(self, text="发送数据", command=self.send_data)
        self.send_button.pack()

        self.pack()

    def send_data(self):
        data = 10
        # 传递数据
        self.b_instance.process_data(data)

    def on_result_change(self, *args):
        result = self.shared_var.get()
        print(f"A 收到结果: {result}")


# 使用示例
root = tk.Tk()
app = A(root)
root.mainloop()
